package com.jrx.anytxn.atom.service.impl;

import com.jrx.anytxn.accounting.entity.GaProductSummaryTransition;
import com.jrx.anytxn.accounting.mapper.ext.ExtGaProductSummaryTransitionMapper;
import com.jrx.anytxn.accounting.service.IProductSummaryService;
import com.jrx.anytxn.atom.service.IDealProductSummaryService;
import com.jrx.anytxn.common.constant.CommonContants;
import com.jrx.anytxn.transaction.config.CustBatchConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @program: anytxn-cf-parent
 * @description:
 * @author: xiaoyu.jing
 * @date: 2021-01-05 16:24
 **/
@Service
public class DealProductSummaryServiceImpl implements IDealProductSummaryService {

    private static Map<String, GaProductSummaryTransition> productSummeryMap;

    @Autowired
    private CustBatchConfig custBatchConfig;
    @Autowired
    private HashOperations<String, String, Object> redisHash;
    @Autowired
    private IProductSummaryService productSummaryService;
    @Resource
    private ExtGaProductSummaryTransitionMapper extGaProductSummaryTransitionMapper;


    private static ReentrantLock lock = new ReentrantLock();


    /**
     * 获取，修改缓存map加锁
     *
     * @param transition
     */
    @Override
    public void putTransition(GaProductSummaryTransition transition) {
        if (null == transition) {
            return;
        }

        lock.lock();
        String redisKey = String.format(CommonContants.PRODUCT_SUMMARY_REDIS_KEY,custBatchConfig.getBatchTaskId());
        try {
            String productId = transition.getProductId();
            if (null == productSummeryMap.get(productId)) {
                productSummeryMap.put(productId, transition);
            } else {
                productSummeryMap.get(productId).add(transition);
            }
            redisHash.putAll(redisKey, productSummeryMap);
        } finally {
            lock.unlock();
        }

    }

    @Override
    public void writeProductSummary() throws Exception{
        List<GaProductSummaryTransition> transitionList = productSummaryService.summary(productSummeryMap, custBatchConfig.getBatchTaskId());
        if (org.apache.commons.collections.CollectionUtils.isNotEmpty(transitionList)) {
            extGaProductSummaryTransitionMapper.batchInsert(transitionList);

        }
    }

    @PostConstruct
    public void initMap() {
        String redisKey = String.format(CommonContants.PRODUCT_SUMMARY_REDIS_KEY,custBatchConfig.getBatchTaskId());
        Object obj = redisHash.entries(redisKey);
        if (obj == null) {
            productSummeryMap = new ConcurrentHashMap<>();
        } else {
            productSummeryMap = (Map<String, GaProductSummaryTransition>) obj;
        }
    }
}
